from PyQt5 import QtWidgets
from SalaryCalculatorUi import Ui_Form
import sys

class SalaryCalculator(QtWidgets.QWidget,Ui_Form):
    def __init__(self):
        super(SalaryCalculator,self).__init__()
        self.setupUi(self)

        self.setWindowTitle("Salary Calculator")

        self.pushButton.clicked.connect(self.Go)
        
    def getvalue(self,info):
        if (info == None) or (info.text()=='') :
            return 0
        else:
            return round(float(info.text()),2)

    def Go(self):
        
        bx = self.getvalue(self.tableWidget.item(0,1))
        nzgz = self.getvalue(self.tableWidget.item(1,1))
        gwjt = self.getvalue(self.tableWidget.item(2,1))
        tbjt = self.getvalue(self.tableWidget.item(3,1))
        qtgf = self.getvalue(self.tableWidget.item(4,1))
        jxjj = self.getvalue(self.tableWidget.item(5,1))
        mbos = self.getvalue(self.tableWidget.item(6,1))
        
        ylbx = self.getvalue(self.tableWidget.item(0,3))
        yilbx = self.getvalue(self.tableWidget.item(1,3))
        zfgjj = self.getvalue(self.tableWidget.item(2,3))
        ghhf = self.getvalue(self.tableWidget.item(3,3))
        sdf = self.getvalue(self.tableWidget.item(4,3))
        gzsh = self.getvalue(self.tableWidget.item(5,3))
        ykhj = self.getvalue(self.tableWidget.item(12,3))

        overtime150_time = self.getvalue(self.lineEdit)
        overtime200_time = self.getvalue(self.lineEdit_2)
        overtime300_time = self.getvalue(self.lineEdit_3)

        #calculate overtime_salary
        over150 = round(round((bx + nzgz + gwjt)/21.75/8*1.5 ,2)* overtime150_time , 2)
        self.tableWidget.item(7,1).setText(str(over150))
        over200 = round(round((bx + nzgz + gwjt)/21.75/8*2 ,2) * overtime200_time , 2)
        self.tableWidget.item(8,1).setText(str(over200))
        over300 = round(round((bx + nzgz + gwjt)/21.75/8*3 ,2) * overtime300_time ,2)
        self.tableWidget.item(9,1).setText(str(over300))
        
        #calculate total salary
        total_salary = bx + nzgz + gwjt + tbjt + qtgf + jxjj + mbos + over150 + over200 + over300
        self.tableWidget.item(12,1).setText(str(total_salary))

        #calculate tax
        insurance = ylbx + yilbx + zfgjj
        tax_salary = round(tax(total_salary,insurance),2)
        self.tableWidget.item(5,3).setText(str(tax_salary))

        #calculate deduction
        deduction_salary = round(insurance + ghhf + sdf + tax_salary,2)
        self.tableWidget.item(12,3).setText(str(deduction_salary))

        #calculate salary
        salary = round(total_salary - deduction_salary ,2)
        self.tableWidget.item(13,1).setText(str(salary))
        return
    
def tax(salary,insurance,freepart=3500):
    if (salary - insurance - freepart) <=1500:
        rate,free = 0.03,0
    elif (salary - insurance - freepart)<=4500:
        rate,free = 0.10,105
    elif (salary - insurance - freepart)<=9000:
        rate,free = 0.20,555
    elif (salary - insurance - freepart)<=35000:
        rate,free = 0.25,1005
    elif (salary - insurance - freepart)<=55000:
        rate,free = 0.30,2755
    elif (salary - insurance - freepart)<=80000:
        rate,free = 0.35,5505
    else:
        rate,free = 0.45,13505
    return (salary - insurance - freepart)*rate - free

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    salarycalculator = SalaryCalculator()
    salarycalculator.show()
    sys.exit(app.exec())
    
